﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_Modules_sub_AddNew : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
        this.btnCancel.Attributes.Add("onclick","return ClearAll()");

        if (!this.IsPostBack) {
            ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
        }
    }

    private Boolean CheckNick(String strNick,DamiWebDataContext db) {
        Boolean bBool = false;
        var query = from p in db.tbl_Nick_Yahoos
                    where p.Nickname == strNick
                    select p;
        if (query.Count<tbl_Nick_Yahoo>() > 0)
            bBool = true;
        return bBool;
    }

    private void AddNew() {
        String strMess = "Nick name này đã tồn tại!";
        int flag = -1;
            try
            {
                WebData data = new WebData();
                DataLibrary.tbl_TopUrl item = new DataLibrary.tbl_TopUrl();
                item.Url = this.txtUrl.Text.Trim();
                item.TopUrl = txtSubUrl.Text.Trim();
                item.Content_title = txtContent_title.Text;
                item.TopText = txtSubText.Text;
                flag= data.tbl_TopUrls.InsertOnSubmit(item);
                strMess = Message.Insert_err;
            }
            catch (Exception err)
            {
                strMess = Message.Insert_err + "! vì lỗi " + err.Message.ToString();
            }
        
        if(flag>0)
            Response.Redirect(ViewState["URLReferrer"].ToString());
        else
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + strMess + "');", true);
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        
            AddNew();
        

    }
}